<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @property bool $recommen 推荐:1=是,2=否
 * @property float $original_price 原价
 * @property int $class_id 类目
 * @property int $deletetime
 * @property int $id
 * @property int $sales 销量
 * @property string $content 商品描述
 * @property string $createtime
 * @property string $images 商品图片
 * @property string $name 商品名称
 * @property-read \app\model\GoodsClass $class
 * @property-read \app\model\GoodsSpec[] $spec
 * @property-read \app\model\UserCollect $user_collect
 * @property-read mixed $images_text
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 * @mixin \think\Model
 */
class Goods extends Model
{
	use SoftDelete;
	//
	protected $createTime = 'createtime';
	protected $deleteTime = 'deletetime';
	
	protected $append = [
		'images_text'
	];
	
	function getImagesTextAttr($value,$data)
	{
		$value = $value ?? ($data['images'] ?? '');
		return $value?explode(',', $value):[];
	}
	
	function spec()
	{
		return $this->hasMany(GoodsSpec::class,'goods_id');
	}
	
	function class()
	{
		return $this->belongsTo(GoodsClass::class,'class_id');
	}
	
	public function userCollect()
	{
		return $this->hasOne(UserCollect::class,'goods_id');
	}
}
